package com.cskaoyan.market.controller.wx;

import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.service.WxTopicService;
import com.cskaoyan.market.util.ResponseUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/topic")
public class WxTopicController {

    @Autowired
    WxTopicService wxTopicService;

    // 专题列表
    @GetMapping("list")
    public Object list() {
        List<MarketTopic> list = wxTopicService.list();
        return ResponseUtil.okList(list);
    }

    // 专题详情
    @GetMapping("detail")
    public Object detail(Integer id) {
        // 获取专题详情
        MarketTopic topic = wxTopicService.detail(id);
        if (topic != null) {
            Map<String, Object> data = new HashMap<>();
            data.put("topic", topic);
            data.put("goods", new ArrayList<>());
            return ResponseUtil.ok(data);
        } else {
            return ResponseUtil.fail(-1, "没有找到相关专题详情");
        }
    }

    // 相关专题
    @GetMapping("related")
    public Object related(Integer id,
                          @RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "4") Integer limit) {
        // 获取相关专题
        PageInfo<MarketTopic> pageInfo = wxTopicService.related(id, page, limit);
        // 返回相关专题
        if (pageInfo != null) {
            return ResponseUtil.okList(pageInfo.getList());
        } else {
            return ResponseUtil.fail(-1, "没有找到相关专题");
        }
    }
}
